package cn.sanli.manage.service.impl;

import cn.sanli.manage.ex.ServiceException;
import cn.sanli.manage.mapper.data1.OrganizationMapper;
import cn.sanli.manage.pojo.dto.System.OrganizationDTO;
import cn.sanli.manage.pojo.dto.System.UpdateOrganizationDTO;
import cn.sanli.manage.pojo.entity.Organization;
import cn.sanli.manage.service.OrganizationService;
import cn.sanli.manage.utils.OrganizationUtils;
import cn.sanli.manage.web.ServiceCode;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 奖惩组织机构(organization)表服务实现类
 *
 * @author makejava
 * @since 2023-12-04 15:33:22
 */
@Service("organizationService")
public class OrganizationServiceImpl implements OrganizationService {
    @Resource
    private OrganizationMapper organizationMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Organization queryById(Integer id) {
        return this.organizationMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增数据
     *
     * @param organization 实例对象
     * @return 实例对象
     */
    @Override
    public Organization insert(Organization organization) {
        this.organizationMapper.insert(organization);
        return organization;
    }

    /**
     * 修改数据
     *
     * @param organization 实例对象
     * @return 实例对象
     */
    @Override
    public Organization update(Organization organization) {
        this.organizationMapper.update(organization);
        return this.queryById(organization.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.organizationMapper.deleteByPrimaryKey(id) > 0;
    }

    /**
     * 查询全部大口和部门
     * @return
     */
    @Override
    public List<Organization> findNodes() {
       //查询所有组织信息
     List<Organization> organizationList = organizationMapper.selectAll();
        //如果查询结果为空，则返回null
        if (CollectionUtils.isEmpty(organizationList)) return null;
        //构建树形数据
        List<Organization> treeList = OrganizationUtils.buildTree(organizationList);
        return treeList;
    }

    /**
     * 添加组织结构
     * @param organizationDTO 添加信息
     */
    @Override
    public void saveOrganization(OrganizationDTO organizationDTO) {
        Organization organization = new Organization();
        organization.setName(organizationDTO.getName());
        organization.setParentId(organizationDTO.getParentId());
        organization.setRemark(organizationDTO.getRemark());
        organizationMapper.insertAll(organization);
    }

    /**
     * 修改组织结构
     * @param organizationDTO 修改信息
     */
    @Override
    public void updateOrganization(UpdateOrganizationDTO organizationDTO) {
        Organization organization = new Organization();
        organization.setName(organizationDTO.getName());
        organization.setParentId(organizationDTO.getParentId());
        organization.setRemark(organizationDTO.getRemark());
        organizationMapper.updateSelective(organization);
    }

    /**
     * 删除组织结构
     * @param id 组织结构id
     */
    @Override
    public void deleteOrganization(Integer id) {
        int count = organizationMapper.countByParentId(id);   // 先查询是否存在子节点，如果存在不允许进行删除
        if (count > 0){
            throw new ServiceException(ServiceCode.ERROR_DELETE, "该节点下有子节点，不可以删除");
        }
        // 不存在子节点直接删除
        organizationMapper.updateIsDeleteById(id);
    }
}

